#include "background.h"
#include "game.h"
#include <iostream>
#include <ostream>
#include <raylib.h>

BackGround::BackGround()
{
    this->speedY = 0.1;
    // 背景
    this->bg = LoadTexture("resources/Background/Blue.png");
    // 边缘
    this->terrain = LoadTexture("resources/Terrain/Terrain (16x16).png");
    // 尘埃
    this->dust[0] = LoadTexture("resources/Other/Dust Particle.png");
    this->dust[1] = LoadTexture("resources/Other/Shadow.png");
    this->dust[2] = LoadTexture("resources/Other/Transition.png");
    this->hadDust = false;
    this->dustTime = sizeof(this->dust) / sizeof(this->dust[0]);
    this->dustRemainTime = this->dustTime;
}

BackGround::~BackGround()
{
    for (Texture2D d : this->dust)
    {
        UnloadTexture(d);
    }
    UnloadTexture(this->terrain);
    UnloadTexture(this->bg);
}

void BackGround::Draw()
{
    float frameTime = GetFrameTime() * 100;
    this->y += frameTime * this->speedY;
    if (this->y > GetScreenHeight())
    {
        this->y = 0;
    }
    for (int x = 0; x <= GetScreenWidth(); x += this->bg.width)
    {
        for (int y = this->y - GetScreenHeight(); y <= GetScreenHeight(); y += this->bg.height)
        {
            DrawTexture(this->bg, x, y, WHITE);
        }
    }

    for (int x = 0; x <= GetScreenWidth(); x += 8)
    {
        for (int y = 0; y <= GetScreenHeight(); y += 8)
        {
            // 上下两条水平边
            if (y == 0)
            {
                DrawTextureRec(this->terrain, Rectangle{0, 0, 48, 8}, Vector2{float(x), float(y)}, WHITE);
            }
            if (y == GetScreenHeight() - 8)
            {
                DrawTextureRec(this->terrain, Rectangle{0, 40, 48, 8}, Vector2{float(x), float(y)}, WHITE);
            }

            // 左右两条竖直边
            if (x == 0)
            {
                DrawTextureRec(this->terrain, Rectangle{0, 72, 8, 32}, Vector2{float(x), float(y)}, WHITE);
            }
            if (x == GetScreenWidth() - 8)
            {
                DrawTextureRec(this->terrain, Rectangle{40, 72, 8, 32}, Vector2{float(x), float(y)}, WHITE);
            }
        }
    }
    // 四个点
    DrawTextureRec(this->terrain, Rectangle{0, 64, 8, 8}, Vector2{0, 0}, WHITE);
    DrawTextureRec(this->terrain, Rectangle{40, 64, 8, 8}, Vector2{float(GetScreenWidth() - 8), 0}, WHITE);
    DrawTextureRec(this->terrain, Rectangle{0, 104, 8, 8}, Vector2{0, float(GetScreenHeight() - 8)}, WHITE);
    DrawTextureRec(this->terrain, Rectangle{40, 104, 8, 8},
                   Vector2{float(GetScreenWidth() - 8), float(GetScreenHeight() - 8)}, WHITE);

    // 尘埃
    if (this->hadDust)
    {
        int index = int(this->dustRemainTime) % (sizeof(this->dust) / sizeof(this->dust[0]));
        DrawTexture(this->dust[index], this->dustPosition.x - float(this->dust[index].width) / 2,
                    this->dustPosition.y - float(this->dust[index].height) / 2, WHITE);
        this->dustRemainTime -= GetFrameTime() * 50;
        if (this->dustRemainTime < 0)
        {
            this->hadDust = false;
            this->dustRemainTime = dustTime;
        }
    }
}

void BackGround::EventHandler(GameStatus *gameStatus)
{
    if (IsKeyPressed(KEY_RIGHT))
    {
        this->hadDust = true;
        this->dustPosition = gameStatus->GetStatusData().clbp;
    }
    if (IsKeyPressed(KEY_LEFT))
    {
        this->hadDust = true;
        this->dustPosition = gameStatus->GetStatusData().crbp;
    }
    if (IsKeyPressed(KEY_UP))
    {
        this->hadDust = true;
        this->dustPosition = gameStatus->GetStatusData().cbmp;
    }
}